﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using RN.ERP.DAL;
using RN.ERP.Entity;
using Fly.FlyCommon;
using Fly.Log;
using Fly.Common.Linq;
using RN.ERP.DAL.Context;
using System.Data.Common;
using System.Data.Entity.Infrastructure;

namespace RN.ERP.BLL
{
    public partial class MaterialBLL
    {
        private string _appId = ConfigurationManager.AppSettings["AppID"];

        private MaterialContext baseContext;

        private MaterialContext dbContext
        {
            get
            {
                return (MaterialContext)this.baseContext;
            }
        }

        public MaterialBLL()
        {
            this.baseContext = new MaterialContext();
        }

        #region 物资属性模块

        public DTO.OperationResult AddMaterialProperty(DTO.MateProperty property)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            MateProperty mProperty = new MateProperty();
            EntityHelper.ConvertEntity2OtherEntity<DTO.MateProperty, MateProperty>(property, mProperty);
            mProperty.dCDate = DateTime.Now;
            mProperty.cMPCode = GetCode("B_MateProperty", "cMPCode", 3);
            try
            {
                mProperty = dbContext.DbMateProperty.Add(mProperty);
                if (dbContext.SaveChanges() > 0)
                {
                    result.Result = true;
                    result.Message = "物资属性添加成功！";
                    UserLogManager.Log(new User_Log()
                    {
                        AppId = _appId,
                        ObjectId = mProperty.id.ToString(),
                        Content = "添加物资",
                        FunctionId = FunctionId.MaterialProperty.ToString(),
                        FunctionType = FunctionType.Add.ToString(),
                        IsrtDate = DateTime.Now,
                        IsrtUser = mProperty.cCUid
                    });
                }
            }
            catch (Exception ex)
            {
                result.Message = "物资属性添加失败：" + ex.Message;
            }

            return result;
        }

        public DTO.OperationResult AddMPInfo(DTO.MPInfo mpInfo, string userGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            MPInfo m = new MPInfo();
            EntityHelper.ConvertEntity2OtherEntity<DTO.MPInfo, MPInfo>(mpInfo, m);
            m.cMPIInfo = m.cMPIInfo.Replace("\r\n", "!#");
            var query = from mp in dbContext.DbMPInfo
                        where mp.iStatus == 0 && mp.cMPCode == mpInfo.cMPCode
                        orderby mp.id descending
                        select mp.id;
            int index = query.FirstOrDefault();
            m.cMPICode = mpInfo.cMPCode + (index + 1).ToString().PadLeft(3, '0');
            try
            {
                m = dbContext.DbMPInfo.Add(m);
                if (dbContext.SaveChanges() > 0)
                {
                    result.Result = true;
                    result.Message = "物资子属性添加成功！";
                    UserLogManager.Log(new User_Log()
                    {
                        AppId = _appId,
                        ObjectId = m.id.ToString(),
                        Content = "添加物资子属性",
                        FunctionId = FunctionId.MaterialProperty.ToString(),
                        FunctionType = FunctionType.Add.ToString(),
                        IsrtDate = DateTime.Now,
                        IsrtUser = userGuid
                    });
                }
            }
            catch (Exception ex)
            {
                result.Message = "物资子属性添加失败：" + ex.Message;
            }

            return result;
        }

        public DTO.OperationResult EditMaterialProperty(DTO.MateProperty property)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            MateProperty mProperty = dbContext.DbMateProperty.SingleOrDefault(mp => mp.id == property.id && mp.iStatus == 0);
            if (mProperty == null)
            {
                result.Message = "无此物资属性信息！";
                return result;
            }
            mProperty.cMemo = property.cMemo;
            mProperty.cMPCode = property.cMPCode;
            mProperty.cMPName = property.cMPName;
            mProperty.cUUid = property.cUUid;
            mProperty.dUDate = DateTime.Now;
            mProperty.iStatus = property.iStatus;
            try
            {
                if (dbContext.SaveChanges() > 0)
                {
                    result.Result = true;
                    result.Message = "物资属性修改成功！";
                    UserLogManager.Log(new User_Log()
                    {
                        AppId = _appId,
                        Content = "修改物资",
                        ObjectId = mProperty.id.ToString(),
                        FunctionId = FunctionId.MaterialProperty.ToString(),
                        FunctionType = FunctionType.Edit.ToString(),
                        IsrtDate = DateTime.Now,
                        IsrtUser = mProperty.cUUid
                    });
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return result;
        }

        public DTO.OperationResult EditMPInfo(DTO.MPInfo mpInfo, string userGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            MPInfo m = dbContext.DbMPInfo.SingleOrDefault(mp => mp.id == mpInfo.id && mp.iStatus == 0);
            if (m == null)
            {
                result.Message = "无此物资属性信息！";
                return result;
            }
            m.bIsQuery = mpInfo.bIsQuery;
            m.cMemo = mpInfo.cMemo;
            m.cMPIInfo = mpInfo.cMPIInfo.Replace("\r\n", "!#");
            m.cMPIName = mpInfo.cMPIName;
            m.iMPIKind = mpInfo.iMPIKind;
            m.iStatus = mpInfo.iStatus;
            try
            {
                if (dbContext.SaveChanges() > 0)
                {
                    result.Result = true;
                    result.Message = "物资子属性修改成功！";
                    UserLogManager.Log(new User_Log()
                    {
                        AppId = _appId,
                        Content = "修改物资子属性",
                        ObjectId = m.id.ToString(),
                        FunctionId = FunctionId.MaterialProperty.ToString(),
                        FunctionType = FunctionType.Edit.ToString(),
                        IsrtDate = DateTime.Now,
                        IsrtUser = userGuid
                    });
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return result;
        }

        public DTO.OperationResult DeleteMaterialProperty(int id)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            MateProperty mProperty = dbContext.DbMateProperty.SingleOrDefault(mp => mp.id == id && mp.iStatus == 0);
            if (mProperty != null)
            {
                mProperty.iStatus = 1;
                try
                {
                    if (dbContext.SaveChanges() > 0)
                    {
                        result.Result = true;
                        result.Message = "物资属性删除成功！";
                        UserLogManager.Log(new User_Log()
                        {
                            AppId = _appId,
                            Content = "删除物资",
                            ObjectId = id.ToString(),
                            FunctionId = FunctionId.MaterialProperty.ToString(),
                            FunctionType = FunctionType.Delete.ToString(),
                            IsrtDate = DateTime.Now,
                            IsrtUser = mProperty.cCUid
                        });
                    }
                }
                catch (Exception ex)
                {
                    result.Message = "物资属性删除失败：" + ex.Message;
                }
            }
            else
            {
                result.Message = "未找到需要删除的物资属性！";
            }

            return result;
        }

        public DTO.OperationResult DeleteMPInfo(int id, string userGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            MPInfo mProperty = dbContext.DbMPInfo.SingleOrDefault(mp => mp.id == id && mp.iStatus == 0);
            if (mProperty != null)
            {
                mProperty.iStatus = 1;
                try
                {
                    if (dbContext.SaveChanges() > 0)
                    {
                        result.Result = true;
                        result.Message = "物资子属性删除成功！";
                        UserLogManager.Log(new User_Log()
                        {
                            AppId = _appId,
                            Content = "删除物资子属性",
                            ObjectId = id.ToString(),
                            FunctionId = FunctionId.MaterialProperty.ToString(),
                            FunctionType = FunctionType.Delete.ToString(),
                            IsrtDate = DateTime.Now,
                            IsrtUser = userGuid
                        });
                    }
                }
                catch (Exception ex)
                {
                    result.Message = "物资子属性删除失败：" + ex.Message;
                }
            }
            else
            {
                result.Message = "未找到需要删除的物资属性！";
            }

            return result;
        }

        public DTO.MateProperty GetMaterialProperty(int id)
        {
            MateProperty mProperty = dbContext.DbMateProperty.SingleOrDefault(mp => mp.id == id && mp.iStatus == 0);
            DTO.MateProperty property = new DTO.MateProperty();
            if (mProperty != null)
            {
                EntityHelper.ConvertEntity2OtherEntity<MateProperty, DTO.MateProperty>(mProperty, property);
                ZBDL_Context zDbContext = new ZBDL_Context();
                Guid userGuid = Guid.Empty;
                Guid.TryParse(mProperty.cCUid, out userGuid);
                Entity.ZBDLRY.T_Sys_Users_Zbdl user = zDbContext.T_Sys_Users.SingleOrDefault(u => u.UserGuid == userGuid);
                if (user != null)
                {
                    property.cCUserName = user.UserName;
                }

                Guid.TryParse(mProperty.cUUid, out userGuid);
                Entity.ZBDLRY.T_Sys_Users_Zbdl user1 = zDbContext.T_Sys_Users.SingleOrDefault(u => u.UserGuid == userGuid);
                if (user1 != null)
                {
                    property.cUUserName = user1.UserName;
                }
                return property;
            }
            else
            {
                return null;
            }
        }

        public DTO.MPInfo GetMPinfo(int id)
        {
            MPInfo m = dbContext.DbMPInfo.SingleOrDefault(mp => mp.id == id && mp.iStatus == 0);
            DTO.MPInfo mpinfo = new DTO.MPInfo();
            if (m != null)
            {
                EntityHelper.ConvertEntity2OtherEntity<MPInfo, DTO.MPInfo>(m, mpinfo);
                mpinfo.cMPIInfo = mpinfo.cMPIInfo.Replace("!#", "\r\n");
                return mpinfo;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据物资类型编号获取物资属性。
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public List<DTO.MateProperty> GetMaterialProperties(
            int pageCount,
            int pageSize,
            out int totalCount)
        {
            var query = from mp in dbContext.DbMateProperty
                        where mp.iStatus == 0
                        select new DTO.MateProperty()
                        {
                            cCUid = mp.cCUid,
                            cMemo = mp.cMemo,
                            cMPCode = mp.cMPCode,
                            cMPName = mp.cMPName,
                            cUUid = mp.cUUid,
                            dCDate = mp.dCDate,
                            dUDate = mp.dUDate,
                            id = mp.id,
                            iStatus = mp.iStatus
                        };
            totalCount = query.Count();
            if (pageSize > 0)
            {
                query = query.OrderByDescending(m => m.dCDate).Skip(pageCount * pageSize).Take(pageSize);
            }
            return query.ToList();
        }

        public List<DTO.MPInfo> GetMPListByPropertyId(
            int pid,
            int pageCount,
            int pageSize,
            out int totalCount)
        {
            var query = from minfo in dbContext.DbMPInfo
                        join mp in dbContext.DbMateProperty on minfo.cMPCode equals mp.cMPCode
                        where mp.iStatus == 0 && minfo.iStatus == 0 && mp.id == pid
                        select new DTO.MPInfo()
                        {
                            bIsQuery = minfo.bIsQuery,
                            strIsQuery = minfo.bIsQuery ? "是" : "否",
                            cMemo = minfo.cMemo,
                            cMPCode = minfo.cMPCode,
                            cMPICode = minfo.cMPICode,
                            cMPIInfo = minfo.cMPIInfo,
                            cMPIName = minfo.cMPIName,
                            cMPName = mp.cMPName,
                            id = minfo.id,
                            iMPIKind = minfo.iMPIKind,
                            iStatus = minfo.iStatus
                        };
            totalCount = query.Count();
            if (pageSize > 0)
            {
                query = query.OrderByDescending(m => m.id).Skip(pageCount * pageSize).Take(pageSize);
            }
            return query.ToList();
        }

        #endregion

        #region 物资分类模块

        public DTO.MateClass GetMaterialClassByCode(string code)
        {
            var query = from mc in dbContext.DbMateClass
                        where mc.iStatus == 0 && mc.cMCCode == code
                        select new DTO.MateClass()
                        {
                            cCUID = mc.cCUID,
                            cMCCode = mc.cMCCode,
                            cMCName = mc.cMCName,
                            cMemo = mc.cMemo,
                            cPMCCode = mc.cPMCCode,
                            cUUID = mc.cUUID,
                            dCDate = mc.dCDate,
                            dUDate = mc.dUDate,
                            id = mc.id,
                            iStatus = mc.iStatus.ToString()
                        };
            return query.SingleOrDefault();
        }

        public DTO.OperationResult AddMaterialClass(DTO.MateClass mclass)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            MateClass eMclass = new MateClass();
            EntityHelper.ConvertEntity2OtherEntity<DTO.MateClass, MateClass>(mclass, eMclass);
            eMclass.dCDate = DateTime.Now;
            eMclass.cMCCode = GetCode("B_MateClass", "cMCCode", 3);
            try
            {
                eMclass = dbContext.DbMateClass.Add(eMclass);
                if (dbContext.SaveChanges() > 0)
                {
                    result.Result = true;
                    result.Message = "物资类型添加成功！";
                    UserLogManager.Log(new User_Log()
                    {
                        AppId = _appId,
                        Content = "添加物资类型",
                        ObjectId = eMclass.id.ToString(),
                        FunctionId = FunctionId.MaterialProperty.ToString(),
                        FunctionType = FunctionType.Add.ToString(),
                        IsrtDate = DateTime.Now,
                        IsrtUser = eMclass.cCUID.ToString()
                    });
                }
            }
            catch (Exception ex)
            {
                result.Message = "物资类型添加失败：" + ex.Message;
            }

            return result;
        }

        #endregion

        #region 物资模块

        public DTO.OperationResult AddMaterial(DTO.Material material)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            Entity.Material m = new Entity.Material();
            EntityHelper.ConvertEntity2OtherEntity<DTO.Material, Entity.Material>(material, m);
            m.cMCode = GetCode("B_Material", "cMCode", 10);
            m.dCDate = DateTime.Now;
            m.dUDate = DateTime.Now;
            m.dMEvaluate = material.dMEvaluate;
            m.iStatus = 0;
            try
            {
                m = dbContext.DbMaterial.Add(m);
                if (dbContext.SaveChanges() > 0)
                {
                    result.Result = true;
                    result.Message = "物资添加成功！";
                    result.ReturnValue = m.cMCode;
                    UserLogManager.Log(new User_Log()
                    {
                        AppId = _appId,
                        Content = "添加物资",
                        ObjectId = m.id.ToString(),
                        FunctionId = FunctionId.MaterialInfo.ToString(),
                        FunctionType = FunctionType.Add.ToString(),
                        IsrtDate = DateTime.Now,
                        IsrtUser = m.cCUid
                    });
                }
            }
            catch (Exception ex)
            {
                result.Message = "物资添加失败：" + ex.Message;
            }

            return result;
        }

        public DTO.OperationResult SaveMInfo(
            List<DTO.MInfo> minfos,
            string cMCode,
            string userGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };
            DbConnection con = ((IObjectContextAdapter)dbContext).ObjectContext.Connection;
            con.Open();
            using (DbTransaction tran = con.BeginTransaction())
            {
                try
                {
                    var mis = dbContext.DbMInfo.Where(mi => mi.cMCode == cMCode).ToList();

                    foreach (DTO.MInfo min in minfos)
                    {
                        MInfo m = mis.SingleOrDefault(minfo => minfo.cMCode == min.cMCode && minfo.cMPICode == min.cMPICode && minfo.iStatus == 0);
                        if (m != null)
                        {
                            m.cMemo = min.cMemo;
                            m.cMPIText = min.cMPIText;
                            m.iStatus = min.iStatus;
                        }
                        else
                        {
                            MInfo m2 = new MInfo();
                            EntityHelper.ConvertEntity2OtherEntity<DTO.MInfo, MInfo>(min, m2);
                            m2.cMCode = cMCode;
                            dbContext.DbMInfo.Add(m2);
                        }
                        dbContext.SaveChanges();
                    } tran.Commit();
                    result.Result = true;
                    result.Message = "物资数据入库成功！";
                    UserLogManager.Log(new User_Log()
                    {
                        AppId = _appId,
                        Content = "添加物资子属性",
                        FunctionId = FunctionId.MInfo.ToString(),
                        FunctionType = FunctionType.Add.ToString(),
                        IsrtDate = DateTime.Now,
                        IsrtUser = userGuid
                    });
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    //插入日志表
                    SysLogManager.Log(new Sys_Log()
                    {
                        ErrorMessage = ex.Message,
                        LogDate = DateTime.Now
                    });
                    result.Message = "物资添加失败：" + ex.Message;
                    result.Result = false;
                    result.ReturnValue = null;
                }
            }

            return result;
        }

        public DTO.OperationResult EdtiMaterial(DTO.Material material)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            Entity.Material mate = dbContext.DbMaterial.SingleOrDefault(m => m.id == material.id && m.iStatus == 0);
            if (mate == null)
            {
                result.Message = "无此物资信息！";
                return result;
            }

            mate.bIsAssets = material.bIsAssets;
            mate.bIsBorrow = material.bIsBorrow;
            mate.bIsSale = material.bIsSale;
            mate.bIsSample = material.bIsSample;
            mate.cMCCode = material.cMCCode;
            mate.cMemo = material.cMemo;
            mate.cMName = material.cMName;
            mate.cUnit = material.cUnit;
            mate.dMEvaluate = material.dMEvaluate;
            mate.iMNum = material.iMNum;

            try
            {
                if (dbContext.SaveChanges() > 0)
                {
                    result.Result = true;
                    result.Message = "物资修改成功！";
                    result.ReturnValue = mate.cMCode;
                    UserLogManager.Log(new User_Log()
                    {
                        AppId = _appId,
                        Content = "修改物资",
                        ObjectId = mate.id.ToString(),
                        FunctionId = FunctionId.MaterialInfo.ToString(),
                        FunctionType = FunctionType.Edit.ToString(),
                        IsrtDate = DateTime.Now,
                        IsrtUser = mate.cUUid
                    });
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
            }

            return result;
        }

        public DTO.OperationResult DelteMaterial(int id, string userGuid)
        {
            DTO.OperationResult result = new DTO.OperationResult() { Result = false };

            Entity.Material mProperty = dbContext.DbMaterial.SingleOrDefault(mp => mp.id == id && mp.iStatus == 0);
            if (mProperty != null)
            {
                mProperty.iStatus = 1;
                try
                {
                    if (dbContext.SaveChanges() > 0)
                    {
                        result.Result = true;
                        result.Message = "物资删除成功！";
                        UserLogManager.Log(new User_Log()
                        {
                            AppId = _appId,
                            Content = "删除物资",
                            ObjectId = id.ToString(),
                            FunctionId = FunctionId.MaterialInfo.ToString(),
                            FunctionType = FunctionType.Delete.ToString(),
                            IsrtDate = DateTime.Now,
                            IsrtUser = userGuid
                        });
                    }
                }
                catch (Exception ex)
                {
                    result.Message = "物资删除失败：" + ex.Message;
                }
            }
            else
            {
                result.Message = "未找到需要删除的物资！";
            }

            return result;
        }

        public DTO.Material GetMaterial(int id)
        {
            Entity.Material m = dbContext.DbMaterial.SingleOrDefault(mp => mp.id == id && mp.iStatus == 0);
            DTO.Material mpinfo = new DTO.Material();
            if (m != null)
            {
                EntityHelper.ConvertEntity2OtherEntity<Entity.Material, DTO.Material>(m, mpinfo);
                string code = m.cMCode;
                var mClass = dbContext.DbMateClass.FirstOrDefault(m1 => m1.cMCCode == m.cMCCode && m1.iStatus == 0);
                if (mClass != null)
                {
                    mpinfo.cMCName = mClass.cMCName;
                }
                var query = from mi in dbContext.DbMInfo
                            where mi.cMCode == code && mi.iStatus == 0
                            select new DTO.MInfo()
                            {
                                cMCode = mi.cMCode,
                                cMemo = mi.cMemo,
                                cMPICode = mi.cMPICode,
                                cMPIText = mi.cMPIText,
                                id = mi.id,
                                iStatus = mi.iStatus
                            };
                mpinfo.Minfos = query.ToList();
                return mpinfo;
            }
            else
            {
                return null;
            }
        }

        public List<DTO.Material> GetMateraials(
            int pageCount,
            int pageSize,
            out int totalCount)
        {
            var query = from material in dbContext.DbMaterial
                        join mclass in dbContext.DbMateClass on material.cMCCode equals mclass.cMCCode
                        where material.iStatus == 0
                        select new DTO.Material()
                        {
                            bIsAssets = material.bIsAssets,
                            bIsBorrow = material.bIsAssets,
                            bIsSale = material.bIsAssets,
                            bIsSample = material.bIsAssets,
                            cCUid = material.cCUid,
                            cMCCode = material.cMCCode,
                            cMCName = mclass.cMCName,
                            cMCode = material.cMCode,
                            cMemo = material.cMemo,
                            cMName = material.cMName,
                            cMPCode = material.cMPCode,
                            cPicURL = material.cPicURL,
                            cUnit = material.cUnit,
                            cUUid = material.cUUid,
                            dCDate = material.dCDate,
                            dMEvaluate = material.dMEvaluate,
                            dUDate = material.dUDate,
                            id = material.id,
                            iMNum = material.iMNum,
                            iStatus = material.iStatus
                        };
            totalCount = query.Count();
            if (pageSize > 0)
            {
                query = query.OrderByDescending(m => m.id).Skip(pageCount * pageSize).Take(pageSize);
            }
            return query.ToList();
        }

        public List<DTO.Material> GetMateraials(
            DTO.QueryMaterialCondition condition,
            int pageCount,
            int pageSize,
            out int totalCount)
        {
            var query = from material in dbContext.DbMaterial
                        join mclass in dbContext.DbMateClass on material.cMCCode equals mclass.cMCCode
                        join minfo in dbContext.DbMInfo on material.cMCode equals minfo.cMCode
                        where material.iStatus == 0
                        select new DTO.Material()
                        {
                            bIsAssets = material.bIsAssets,
                            bIsBorrow = material.bIsAssets,
                            bIsSale = material.bIsAssets,
                            bIsSample = material.bIsAssets,
                            cCUid = material.cCUid,
                            cMCCode = material.cMCCode,
                            cMCName = mclass.cMCName,
                            cMCode = material.cMCode,
                            cMemo = material.cMemo,
                            cMName = material.cMName,
                            cMPCode = material.cMPCode,
                            cPicURL = material.cPicURL,
                            cUnit = material.cUnit,
                            cUUid = material.cUUid,
                            dCDate = material.dCDate,
                            dMEvaluate = material.dMEvaluate,
                            dUDate = material.dUDate,
                            id = material.id,
                            iMNum = material.iMNum,
                            iStatus = material.iStatus
                        };
            query = query.BuildCondition<DTO.Material>(condition);
            totalCount = query.Count();
            if (pageSize > 0)
            {
                query = query.OrderByDescending(m => m.id).Skip(pageCount * pageSize).Take(pageSize);
            }
            return query.ToList();
        }

        #endregion

        #region 辅助功能

        /// <summary>
        /// 根据表名，字段名及最大长度生成编号。
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="columnName"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        internal string GetCode(string tableName, string columnName, int maxLength)
        {
            string strSql = string.Format("select top 1 {0} from {1} order by id desc", columnName, tableName);
            string result = dbContext.Database.SqlQuery<string>(strSql).FirstOrDefault();
            if (string.IsNullOrEmpty(result))
            {
                return "1".PadLeft(maxLength, '0');
            }
            else
            {
                int index = 1;
                int.TryParse(result, out index);
                return (index + 1).ToString().PadLeft(maxLength, '0');
            }
        }

        public List<User_Log> GetLog(string objectId, string funcId)
        {
            var logs = UserLogManager.GetLog(_appId, funcId, string.Empty, objectId, string.Empty, string.Empty, string.Empty).OrderByDescending(l => l.IsrtDate).ToList();
            string userInfo = string.Empty;
            foreach (string isrtUser in logs.Select(l => l.IsrtUser).Distinct())
            {
                userInfo += "'" + isrtUser + "',";
            }
            userInfo = userInfo.TrimEnd(',');
            if (!string.IsNullOrEmpty(userInfo))
            {
                var kv = dbContext.Database.SqlQuery<KeyValue>("select userguid key,name value from t_sys_users where userguid in (" + userInfo + ")");
                foreach (User_Log log in logs)
                {
                    var kvi = kv.SingleOrDefault(k => k.Key == log.IsrtUser);
                    if (kvi != null)
                    {
                        log.IsrtUserName = kvi.Value;
                    }
                }
            }
            return logs;
        }

        #endregion
    }
}
